package com.yuesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuesystem.common.CustomException;
import com.yuesystem.dto.FeedbackFront;
import com.yuesystem.entity.Notice;
import com.yuesystem.mapper.NoticeMapper;
import com.yuesystem.service.NoticeService;
import com.yuesystem.service.UserService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 * 公告接口实现类.
 */
@Service
@Slf4j
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
  @Autowired
  private NoticeService noticeService;
  
  /**
   * 发布帖子.
   *
   * @param notice 帖子
   */
  @Override
  public void addNotice(Notice notice) {
    if (notice.getTitle().isEmpty()) {
      throw new CustomException("公告标题不能为空，请编辑公告标题后重试！");
    }
    if (notice.getContent().isEmpty()) {
      throw new CustomException("公告内容不能为空，请编辑公告内容后重试！");
    }
    notice.setStatus(1);
    this.save(notice);
  }
  
  /**
   * 获取已发布的帖子.
   */
  @Override
  public Page<Notice> getPage(FeedbackFront feedbackFront) {
    int page = feedbackFront.getPage();
    int pageSize = feedbackFront.getPageSize();
    Page<Notice> dtoPage = new Page<>(page, pageSize);
    QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("status", 1);
    queryWrapper.orderByDesc("create_time");
    List<Notice> list = noticeService.list(queryWrapper);
    dtoPage.setRecords(list);
    dtoPage.setTotal(list.size());
    return dtoPage;
  }
}
